package trunk.gui;

//import com.sun.xml.internal.bind.v2.TODO;
//import sun.awt.SunHints;
import trunk.control.Control;
import trunk.control.Launcher;
import trunk.res.R;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

/**
 * panel used for adjust how we display the simulations, speed, color, details and so on
 * Created by IntelliJ IDEA.
 * User: yipeipei
 * Date: 11/28/11
 * Time: 10:55 PM
 * To change this template use File | Settings | File Templates.
 */
public class ParameterPanel extends JPanel implements ChangeListener, ActionListener {
    private JButton start;   //start the simulation at the very beginning
    public JButton pause;   //pause and continue the simulation
    private JRadioButton singleStep;   //single step will auto pause after one operation, and wait user to click the start button to continue
    private JSlider speedSlider;

    private static String START = "开始";
    private static String STOP = "停止";
    private static String PAUSE = "暂停";
    private static String CONTINUE = "继续";
    private static String SINGLE_STEP = "单步";


    public ParameterPanel() {
        super(true);
        this.setBounds(
                R.attribute.SIM_PANEL_WIDTH,
                R.attribute.INSTRUCTION_PANEL_HEIGHT,
                R.attribute.PARAMETER_PANEL_WIDTH,
                R.attribute.PARAMETER_PANEL_HEIGHT);
        this.setBorder(R.attribute.BORDER);

        initialize();

        this.setVisible(true);
    }

    /**
     * initializeAuxiliary components
     */
    private void initialize() {
        this.speedSlider = new JSlider(0, 8, 4);
        Control.getInstant().setSpeed(this.speedSlider.getValue());   //parameter transfer

        this.speedSlider.setMajorTickSpacing(2);
        this.speedSlider.setMinorTickSpacing(1);
        this.speedSlider.setSnapToTicks(true);  //set the slider must be slide to a integer
        this.speedSlider.setPaintLabels(true);  //paint number labels
        this.speedSlider.setPaintTicks(true);   //paint ticks
        this.speedSlider.addChangeListener(this);
        this.add(this.speedSlider);


        this.start = new JButton(START);
        this.start.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if(e.getKeyCode() == KeyEvent.VK_ENTER){
                    Launcher.simulator.getParameterPanel().start.doClick();
                }
            }
        });
//        Control.getInstant().setStartFlag(false);
        this.start.addActionListener(this);
        this.add(this.start);

        this.pause = new JButton(PAUSE);
//        Control.getInstant().setPauseFlag(false);
        this.pause.addActionListener(this);
        this.add(this.pause);

        this.singleStep = new JRadioButton(SINGLE_STEP);
//        Control.getInstant().setStepFlag(false);
        this.singleStep.addActionListener(this);
        this.add(this.singleStep);

    }

    public void stateChanged(ChangeEvent e) {
        if (e.getSource() == this.speedSlider) {
            Control.getInstant().setSpeed(this.speedSlider.getValue());
        }
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == this.start) {
            if (e.getActionCommand() == START) {
                Control.getInstant().setStartFlag(true);
                Launcher.simulator.getInstructionPanel().disableSelect();
                this.start.setText(STOP);
            } else if (e.getActionCommand() == STOP) {
                Control.getInstant().setStartFlag(false);
                Launcher.simulator.getInstructionPanel().enableSelect();
                this.start.setText(START);
            }
        } else if (e.getSource() == this.pause) {
            if (e.getActionCommand() == PAUSE) {
                btnPause_Pause();
            } else if (e.getActionCommand() == CONTINUE) {
                btnPause_Continue();
            }
        } else if (e.getSource() == this.singleStep) {
            if (this.singleStep.isSelected()) {
                Control.getInstant().setStepFlag(true);
            } else {
                Control.getInstant().setStepFlag(false);
            }
        }
    }

    public void btnPause_Pause() {
        Control.getInstant().setPauseFlag(true);
        this.pause.setText(CONTINUE);
        this.repaint();
    }

    public void btnPause_Continue() {
        Control.getInstant().setPauseFlag(false);
        this.pause.setText(PAUSE);
        if(Control.getInstant().isStepFlag()){
            Control.getInstant().setStepFlag(false);
        }
    }
}
